/**
 * 加载过度UI类;
 *
 
 */
import QRCode from "./../../lib/QRcode/QRCode";

const { ccclass, property } = cc._decorator;
@ccclass
export default class QRnode extends cc.Component {
	@property(cc.Graphics)
	ctx: cc.Graphics = null;

	@property(cc.Node)
	rim: cc.Node = null;

	fill(data) {
		let str = data.content;
		this.rim.width = data.width;
		this.rim.height = data.height;
		this.QRCreate(str);
	}
	QRCreate(url) {
		var qrcode = new QRCode(-1, 2);
		qrcode.addData(url);
		qrcode.make();

		this.ctx.fillColor = cc.Color.BLACK;
		//块宽高
		var tileW = this.rim.width / qrcode.getModuleCount();
		var tileH = this.rim.height / qrcode.getModuleCount();

		let xDif = this.rim.width / 2;
		let yDif = this.rim.height / 2;
		for (var row = 0; row < qrcode.getModuleCount(); row++) {
			for (var col = 0; col < qrcode.getModuleCount(); col++) {
				if (qrcode.isDark(row, col)) {
					var w = Math.ceil((col + 1) * tileW) - Math.floor(col * tileW);
					var h = Math.ceil((row + 1) * tileW) - Math.floor(row * tileW);
					this.ctx.rect(
						Math.round(col * tileW) - xDif,
						Math.round(row * tileH) - yDif,
						w,
						h
					);
					this.ctx.fill();
				}
			}
		}
	}
}
